home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / MOD2TXT.ZIP / CHAP2.TXT < prev    next >
Text File  |  1987-03-25  |  15KB  |  322 lines

  1.                     Chapter 2 - Getting started in Modula-2
  2.  
  3.  
  4.                          OUR FIRST MODULA-2 PROGRAM
  5.  
  6.              We are ready to look at our first instructional program
  7.         in Modula-2.  Assuming that you have a full screen editor of
  8.         some  type,  load the program PUPPYDOG.MOD and display it on
  9.         your  screen.  It  is  an example of  the  minimum  Modula-2
  10.         program.  There  is  nothing that can be left  out  of  this
  11.         program and still have a compileable, executable program.
  12.  
  13.              The first word in the program,  "MODULE",  is the  name
  14.         that  identifies a module,  and it must be written as  given
  15.         here,  in all capital letters.  During the entire first part
  16.         of  this tutorial,  we will use only this type of a  module.
  17.         There  are  other types but we will not look at any of  them
  18.         until  we  get  to part  III  of  this  tutorial.   Modula-2
  19.         requires  us  to  name  our module so we  give  it  a  name,
  20.         "PuppyDog".   We  could have used any name that qualifies as
  21.         an identifier but we have chosen a name that has nothing  to
  22.         do  with computers as an illustration that any name could be
  23.         used.  In a practical program, you would probably use a name
  24.         that was descriptive of the program in some way.
  25.  
  26.                              WHAT IS AN IDENTIFIER?
  27.  
  28.              An  identifier is a combination of letters and  numbers
  29.         that  Modula-2  uses to identify a variable,  program  name,
  30.         procedure name,  and several other quantities.  In Modula-2,
  31.         an identifier is composed of any number of  characters.  The
  32.         characters   may  be  any  mix  of  alphabetic  and  numeric
  33.         characters,  but  the first character must be an  alphabetic
  34.         character.    The   case  of  the  alphabetic  character  is
  35.         significant  such that "IdentNumber1",  "IDENTNUMBER1",  and
  36.         "IdEnTnUmBeR1" are all different identifiers.   No spaces or
  37.         any other special characters are allowed.
  38.  
  39.                   BACK TO THE PROGRAM UNDER CONSIDERATION
  40.  
  41.              The  "header"  line  is  terminated  with  a  semicolon
  42.         according to the formal definition of Modula-2.  A semicolon
  43.         is  a  statement  separator and many will be used  in  large
  44.         programs.   Following the semicolon,  we come to the program
  45.         itself.  The program statements are enclosed between the two
  46.         words  "BEGIN"  and  "END".   In  this  case  there  are  no
  47.         statements,  but  if there were some,  they would be  placed
  48.         between the two indicated words.   Finally,  the module name
  49.         is repeated after the "END" and it is followed by a  period.
  50.         The  module  name is repeated in order to make  the  program
  51.         easier to understand by clearly marking its limits.  In this
  52.         case  it  really doesn't add to the clarity of the  program,
  53.         but  in a large program it can be of significant help.   The
  54.         period marks the end of the listing and can be thought of as
  55.  
  56.  
  57.                                    Page 8
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                     Chapter 2 - Getting started in Modula-2
  68.  
  69.  
  70.         the period that marks the end of a sentence.
  71.  
  72.              The three words,  MODULE,  BEGIN,  and END, are special
  73.         words  in Modula-2.  They are "reserved words" because  they
  74.         are  used for a specific purpose and cannot be used for  any
  75.         other  purpose.  They are not available for your use in  any
  76.         way except for the defined purpose.   The reserved words  in
  77.         Modula-2  are  always capitalized or the compiler  will  not
  78.         consider them as reserved words.   Remember that  alphabetic
  79.         characters  must  have the correct case in  Modula-2.   Some
  80.         other  languages,  most  notably Pascal,  allow you  to  use
  81.         either case anywhere and it converts them internally so that
  82.         they  are the same.   It would be permissible for you to use
  83.         words  such as "Begin" or "End" as variables in  a  Modula-2
  84.         program,  but it would be very poor programming practice and
  85.         should be avoided.   We will come across many other reserved
  86.         words  in  these  lessons.  There are 40 reserved  words  in
  87.         Modula-2.
  88.  
  89.              You should have learned how to use your compiler by now
  90.         so  you  can  compile and run  this  program.   It  will  do
  91.         nothing,  but  that  is significant in  itself,  because  it
  92.         should  at  least  return to the operating system  after  it
  93.         finishes doing nothing.   That may sound a little silly, but
  94.         it  does  take  a considerable amount  of  effort  to  load,
  95.         transfer control to the program,  and set up linkage back to
  96.         your Disk Operating System.
  97.  
  98.              It  should  be  noted at this time  that  the  Modula-2
  99.         compiler  doesn't care about extra blanks or  linefeeds  and
  100.         the   careful  programmer  will  insert  extra  blanks   and
  101.         linefeeds  as desired in order to make the program easier to
  102.         read.   As you continue to program in Modula-2,  you will no
  103.         doubt  develop  a  style  of your  own  and  hopefully  your
  104.         programs can be read easily by other programmers.
  105.  
  106.                        A PROGRAM THAT DOES SOMETHING
  107.  
  108.              Load  and display the program named WRITESM.MOD for  an
  109.         example  of a Modula-2 program that does  something.   First
  110.         you should notice that the elements of the first program are
  111.         still  here as they will be in every Modula-2 program.   The
  112.         same three reserved words are used here as before,  but  now
  113.         there are some added statements.
  114.  
  115.              The  line  near  the beginning  that  begins  with  the
  116.         reserved  word "FROM" is a special line that must be used in
  117.         any program that accesses external procedures.   We will not
  118.         try to define this line at this time.  We will only say that
  119.         every  external  call in Modula-2 requires a  definition  of
  120.         where to find the procedure.   The module named "InOut" is a
  121.  
  122.  
  123.                                    Page 9
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                     Chapter 2 - Getting started in Modula-2
  134.  
  135.  
  136.         collection  of input and output routines that are  available
  137.         for our use and this line in the program tells the system to
  138.         look  in  the "InOut" collection for  the  procedures  named
  139.         "WriteLn"  and WriteString".   When the program needs  these
  140.         particular  functions to do what we ask it to do,  it  knows
  141.         where to find them.  We will cover the IMPORT list in detail
  142.         later in this tutorial.   Until then, simply use the example
  143.         programs  as  a  guide  when you wish to  write  a  practice
  144.         program.
  145.  
  146.                         OUR FIRST PROGRAM STATEMENTS
  147.  
  148.              Between the BEGIN and END statements,  which we defined
  149.         previously as the place where the actual program is  placed,
  150.         we  have a series of "WriteString" and WriteLn"  statements.
  151.         These  statements are almost self explanatory,  but we  will
  152.         say a few words about them anyway.  Each line is a call to a
  153.         "procedure"  which is a very important feature of  Modula-2.
  154.         A "procedure" is an external servant that does a certain job
  155.         for  us  in  a  well  defined  way.   In  the  case  of  the
  156.         "WriteString", it looks at the string of characters supplied
  157.         to  it and displays the string of characters on the  monitor
  158.         at the current cursor position. In the case of the "WriteLn"
  159.         procedure,  it  serves us by moving the cursor down one line
  160.         on the monitor and moving it to the left side of the screen.
  161.  
  162.              The  parentheses  are  required  for  the   WriteString
  163.         because  it  has  data following it.   The data  within  the
  164.         parentheses  is data supplied to our slave  or  helper.   It
  165.         gets the string of characters between the quotation marks or
  166.         the apostrophes and displays the string on the monitor.  You
  167.         have  a  choice  of delimiters so that you  can  output  the
  168.         delimiters  themselves.  If you desire to output a quotation
  169.         mark to the monitor,  use apostrophes for delimiters, and if
  170.         you wish to output apostrophes, use quotation marks.  If you
  171.         wish  to  output  both,  break the line  up  and  output  it
  172.         piecemeal as in the last example line.
  173.  
  174.              This program should be very clear to you by now.  First
  175.         we tell the system where to get the procedures, then we list
  176.         the procedures in the order required to produce the  desired
  177.         results.   It  should  be  apparent that the  lines  of  the
  178.         program  between the reserved words BEGIN and END are simply
  179.         executed in order.   Compile and run the program and observe
  180.         the output on your monitor.   It should be mentioned at this
  181.         point  that  it is possible to redirect the  output  to  the
  182.         printer  or to a disk file but we will not be doing that for
  183.         quite  some  time.   We will stay with the basic  syntax  of
  184.         Modula-2 for now.
  185.  
  186.  
  187.  
  188.  
  189.                                    Page 10
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                     Chapter 2 - Getting started in Modula-2
  200.  
  201.  
  202.                              MODULA-2 COMMENTS
  203.  
  204.              No program is complete without a few comments  embedded
  205.         in  the  program as notes to the programmer  describing  the
  206.         reasons  for  doing some particular thing.   The  notes  are
  207.         particularly  helpful  to another programmer  who  needs  to
  208.         modify  the program some day.   It is not necessary for  the
  209.         computer to understand the notes and in fact, you don't want
  210.         the computer to try to understand the notes, so you tell the
  211.         compiler to ignore the notes completely.  How to do this  is
  212.         the  object of our next program named MODCOMS.MOD which  you
  213.         should load and display on your monitor.
  214.  
  215.              In Modula-2,  comments are enclosed in pairs of  double
  216.         characters.  The comment is started with the "(*", and ended
  217.         with  the  "*)".   The program on your monitor  has  several
  218.         examples of comments in it.  If the comments were completely
  219.         removed,  the  program would be very similar to the last one
  220.         but  a  lot shorter.   Notice that comments  can  go  nearly
  221.         anywhere  in a program,  even before the header statement or
  222.         after the ending period.   Comments can be used to remove  a
  223.         section  of  program from consideration by the  compiler  so
  224.         that  a  particularly  troublesome section of  code  can  be
  225.         "commented  out" until you solve some of the other  problems
  226.         in  program  debugging.   It is important to  remember  that
  227.         comments  can  be "nested" in Modula-2 so that a section  of
  228.         code  can  be  "commented out" even  if  it  contains  other
  229.         comments.
  230.  
  231.              This  particular program is not meant to be an  example
  232.         of  good commenting.  It is really a sloppy looking  program
  233.         that would need some work to put it into a good  style,  but
  234.         it does illustrate where it is possible to put comments.
  235.  
  236.                            GOOD PROGRAMMING STYLE
  237.  
  238.              Load  and display the program named GOODFORM.MOD for an
  239.         example of a well formatted program.   Since Modula-2 allows
  240.         you to use extra spaces and blank lines freely,  you  should
  241.         use  them  in any way you can to make your programs easy  to
  242.         understand, and therefore easy to debug and modify.  Special
  243.         care  has been given to style in this program and  it  payed
  244.         off  in a very easy to understand program.   Even with  your
  245.         very  limited knowledge of Modula-2 programming you can very
  246.         quickly decipher what it does.  It is so well formatted that
  247.         comments are not needed and they would probably detract from
  248.         its  readability.   No further comment is needed or will  be
  249.         given.   Compile and run this program to see if it does what
  250.         you think it will do.
  251.  
  252.  
  253.  
  254.  
  255.                                    Page 11
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                     Chapter 2 - Getting started in Modula-2
  266.  
  267.  
  268.                            REALLY BAD FORMATTING
  269.  
  270.              Load and display UGLYFORM.MOD for an excellent  example
  271.         of  bad  formatting.   If you can see at a glance what  this
  272.         program  does you deserve the Nobel Prize for  understanding
  273.         software  if  such  a thing exists.   The  syntax  for  this
  274.         program  follows  all of the rules of  Modula-2  programming
  275.         except for good style.   Without saying anything else  about
  276.         this  mess,  I would suggest that you try to compile and run
  277.         it.   You may be surprised to find that it does compile  and
  278.         run,  and in fact it is identical to the last program.  Keep
  279.         in mind that you can add extra blanks and linefeeds anyplace
  280.         you desire in a program to improve its readability.
  281.  
  282.              Hopefully,  the last two programs will be an indication
  283.         to you that good programming style is important and can be a
  284.         tremendous  aid in understanding what a program is  supposed
  285.         to do.   You will develop your own programming style as time
  286.         goes by.   It is good for you to spend some effort in making
  287.         your  program look good,  but don't get too excited about it
  288.         yet.   Initially,  you should expend your effort in learning
  289.         how to program in Modula-2 with reasonable style and  strive
  290.         to improve your style as you go along.  It would be good for
  291.         now  if  you simply tried to copy the style given  in  these
  292.         lessons.
  293.  
  294.         PROGRAMMING EXERCISES
  295.  
  296.         1.   Write  a  program that will display your name  on  the
  297.              monitor.
  298.  
  299.         2.   Write  a  program that will display your name  on  the
  300.              monitor along  with your address, city and state  in 3
  301.              separate lines.
  302.  
  303.         3.   Add a comment to the MODCOMS.MOD file between the words
  304.              "IMPORT"  and  "WriteLn"  to see if the  compiler  will
  305.              allow you to put a comment within a statement.
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.                                    Page 12
  322.